package atm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 改密界面
 */
public class ChangePwdPanel extends JPanel implements ActionListener {
    private ATMFrame atmFrame;
    private ImageIcon sureImg = new ImageIcon("images/buttonSure.jpg");
    private JButton sureBtn = new JButton(sureImg);
    private JLabel oldPwdLabel = new JLabel("请输入旧密码:");
    private JLabel newPwdLabel1 = new JLabel("请输入新密码:");
    private JLabel newPwdLabel2 = new JLabel("请再次输入新密码:");
    private JPasswordField oldPwdField = new JPasswordField();
    private JPasswordField newPwdField1 = new JPasswordField();
    private JPasswordField newPwdField2 = new JPasswordField();
    private Font font = new Font("黑体", Font.BOLD, 26);
    private Color color = new Color(173, 9, 9, 255);

    public ChangePwdPanel(ATMFrame atmFrame) {
        this.atmFrame =  atmFrame;
        this.setLayout(null);
        oldPwdLabel.setBounds(265,200,240,50);
        oldPwdLabel.setFont(font);
        oldPwdLabel.setForeground(color);
        newPwdLabel1.setBounds(265,260,240,50);
        newPwdLabel1.setFont(font);
        newPwdLabel1.setForeground(color);
        newPwdLabel2.setBounds(220,320,240,50);
        newPwdLabel2.setFont(font);
        newPwdLabel2.setForeground(color);
        oldPwdField.setBounds(450,200,250,50);
        newPwdField1.setBounds(450,260,250,50);
        newPwdField2.setBounds(450,320,250,50);
        sureBtn.setBounds(884,240,140,50);
        this.add(oldPwdLabel);
        this.add(newPwdLabel1);
        this.add(newPwdLabel2);
        this.add(oldPwdField);
        this.add(newPwdField1);
        this.add(newPwdField2);
        this.add(sureBtn);
        sureBtn.addActionListener(this);
        this.setOpaque(false);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == sureBtn){
            User user = atmFrame.getLoginUser();
            if (user.getPwd().equals(String.valueOf(oldPwdField.getPassword()))){
                if (String.valueOf(newPwdField1.getPassword()).equals(String.valueOf(oldPwdField.getPassword()))){
                    JOptionPane.showMessageDialog(this,"新密码不能和旧密码相同");
                    return;
                }
                if (String.valueOf(newPwdField1.getPassword()).equals(String.valueOf(newPwdField2.getPassword()))){
                    user.setPwd(String.valueOf(newPwdField1.getPassword()));
                    atmFrame.switchPanel("login");
                    return;
                }
                JOptionPane.showMessageDialog(null,"两次输入不相同");
                return;
            }
            JOptionPane.showMessageDialog(null,"原密码输入不对");
            return;
        }
    }
}
